home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Zoomed Video Driver v1.0 SDK / Tools / PC Card DispNameReg / Src / FormatThisProperty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-02  |  10.0 KB  |  322 lines  |  [TEXT/CWIE]

  1. #include "SystemSoft.h"
  2. /*
  3.     File:        FormatThisProperty.c
  4.  
  5.     Contains:    Store the Name Registry value into the Twist-Down list.
  6.     
  7.     Written by: Martin Minow
  8.  
  9.     Copyright © 1993-95 Apple Computer Inc. All rights reserved.
  10.     
  11.     Change History (most recent first):
  12.     
  13.          4/3/96        Dave Tarabar    SystemSoft Corp
  14.          - no need to display full hex of a driver, limit hex dumps to 10 lines
  15.          - reformat into the style that I like
  16. */
  17.  
  18. #include <stdio.h>
  19. #include "DisplayNameRegistry.h"
  20. #include <ctype.h>
  21. #include <string.h>
  22.  
  23. //    To test for a driver (executable code), look at the first 7 bytes of the property.
  24.  
  25. #define kDriverProperty        "driver,"
  26. #define kAddressProperty    "assigned-addresses"
  27. #define kRegPropery            "reg"
  28. #define IsDriverProperty(property) \
  29.     (strncmp((property), kDriverProperty, 7) == 0)
  30. #define IsAddressProperty(property) (strcmp((property), kAddressProperty) == 0)
  31. #define IsRegProperty(property) (strcmp((property), kRegPropery) == 0)
  32.  
  33. //    The PhysAddressProperty structure from IEEE 1275 is used for "reg" and
  34. //    "assigned-address" properties.
  35.  
  36. typedef struct PhysAddressProperty {
  37.     UInt32            physHi;
  38.     UInt32            physMid;
  39.     UInt32            physLo;
  40.     UInt32            sizeHi;
  41.     UInt32            sizeLo;
  42. } PhysAddressProperty, *PhysAddressPropertyPtr;
  43.  
  44. //    These masks and values decode the physHi word. The format is
  45. //    npt000ss bbbbbbbb dddddfff rrrrrrrr
  46. //    
  47. //    n        1 = Relocatable, 0 = Absolute
  48. //    p        1 = Prefetchable
  49. //    t        1 = Alias
  50. //    ss        Space code (Config, I/O, Mem, 64-bit Mem)    2 bits
  51. //    bbbbbbbb    Bus number                                8 bits
  52. //    ddddd    Device number                                5 bits
  53. //    fff        Function number                                3 bits
  54. //    rrrrrrrr    Register number                         8 bits
  55.  
  56. #define kPhysAbsolute        0x80000000        //    0 if relocatable, 1 if not
  57. #define kPhysPrefetch        0x40000000        //    1 if "prefetchable", 0 if not
  58. #define kPhysAlias            0x20000000        //    1 if alised (for I/O) or below 1MB (Mem)
  59. #define kPhysSpaceMask        0x03000000        //    Config, Mem, I/O, or 64-bit Mem
  60. #define kPhysSpaceShift        24
  61. #define kPhysBusNumberMask    0x00FF0000
  62. #define kPhysBusNumberShift    16
  63. #define kPhysDeviceMask        0x0000F800
  64. #define kPhysDeviceShift    11
  65. #define kPhysFunctionMask    0x00000700
  66. #define kPhysFunctionShift    8
  67. #define kPhysRegisterMask    0x000000FF
  68. #define kPhysRegisterShift    0
  69.  
  70.  
  71. //--------------------------------------------------------------------------------
  72. Boolean FormatStringProperty(RegPropertyValueSize propertySize,
  73.         const void *propertyValue, char *result);
  74. Boolean FormatStringProperty(RegPropertyValueSize propertySize,
  75.         const void *propertyValue, char *result)
  76. {
  77.     Boolean                    isStringProperty, isAscii;
  78.     int                        i;
  79.     register char            *cp;
  80.     
  81.     isStringProperty = FALSE;
  82.     if (propertySize < (kOneLineFormatLength - 3))
  83.     {
  84.         isAscii = TRUE;
  85.         cp = ((char *) propertyValue);
  86.         for (i = 0; isAscii && i < (propertySize - 1); i++)
  87.             isAscii = IS_ASCII_PRINT(cp[i]);
  88.         if (isAscii && cp[i] == '\0')
  89.         {
  90.             sprintf(result, "\"%s\"", cp);
  91.             isStringProperty = TRUE;
  92.         }
  93.     }
  94.     return (isStringProperty);
  95. }
  96.  
  97. //--------------------------------------------------------------------------------
  98. Boolean FormatSmallProperty( RegPropertyValueSize propertySize,
  99.         const void *propertyValue, char *result);
  100. Boolean FormatSmallProperty( RegPropertyValueSize propertySize,
  101.         const void *propertyValue, char *result)
  102. {
  103.     Boolean                    isSmallProperty;
  104.     long                    *lp;
  105.     short                    *sp;
  106.     
  107.     isSmallProperty = FALSE;
  108.     if (propertySize == sizeof (short))
  109.     {
  110.         sp = (short *) propertyValue;
  111.         sprintf(result, "[%04x = %d]", ((int) *sp) & 0xFFFF, (int) *sp);
  112.         isSmallProperty = TRUE;
  113.     }
  114.     if (propertySize == sizeof (long))
  115.     {
  116.         lp = (long *) propertyValue;
  117.         sprintf(result, "[%08x = %ld]",
  118.             lp[0], lp[0]
  119.         );
  120.         isSmallProperty = TRUE;
  121.     }
  122.     else if (propertySize == (sizeof (long) * 2))
  123.     {
  124.         lp = (long *) propertyValue;
  125.         sprintf(result, "[%08x = %ld], [%08x = %ld]",
  126.             lp[0], lp[0], lp[1], lp[1]);
  127.         isSmallProperty = TRUE;
  128.     }
  129.     return (isSmallProperty);
  130. }
  131.  
  132. //--------------------------------------------------------------------------------
  133. //    FormatOneLineProperty tries to format the property value into a single line.
  134. //    It returns TRUE if it succeeds.
  135. Boolean FormatOneLineProperty( RegPropertyValueSize propertySize, 
  136.         const void *propertyValue, char *result);
  137. Boolean FormatOneLineProperty( RegPropertyValueSize propertySize, 
  138.         const void *propertyValue, char *result)
  139. {
  140.     Boolean                    oneLiner;
  141.     
  142.     oneLiner = FormatStringProperty(propertySize, propertyValue, result);
  143.     
  144.     if (oneLiner == FALSE)
  145.         oneLiner = FormatSmallProperty(propertySize, propertyValue, result);
  146.  
  147.     return (oneLiner);
  148. }
  149.  
  150. //--------------------------------------------------------------------------------
  151. //    FormatPhysAddressProperty formats a vector of "reg" and/or "assigned-address"
  152. //    descriptor values, as described in IEEE 1275-1994. The caller has checked that this is,
  153. //    indeed, a "reg" or "assigned-address" property. The "reg" property is the primary
  154. //    mechanism that PCI uses to describe a device's physical memory requirements. The
  155. //    "assigned-address" property binds physical addresses to the actual hardware
  156. //    configuration.
  157. void FormatPhysAddressProperty(PhysAddressPropertyPtr physAddressPropertyPtr,
  158.         short nPhysAddress, TwistDownSiblingSet *twistDownSiblingSetPtr);
  159. void FormatPhysAddressProperty(PhysAddressPropertyPtr physAddressPropertyPtr,
  160.         short nPhysAddress, TwistDownSiblingSet *twistDownSiblingSetPtr)
  161. {
  162.     short                    i;
  163.     unsigned int            busNumber, deviceNumber, functionNumber, registerNumber;
  164.     OSErr                    status;
  165.     char                    *addressType, *isPrefetch, *isAlias, *isAbsolute;
  166.     char                    work[256];
  167.     UInt32                    cellHi;
  168.     
  169.     for (status = noErr, i = 0; status == noErr && i < nPhysAddress; i++)
  170.     {
  171.         cellHi = physAddressPropertyPtr[i].physHi;
  172.         busNumber = (cellHi & kPhysBusNumberMask) >> kPhysBusNumberShift;
  173.         deviceNumber = (cellHi & kPhysDeviceMask) >> kPhysDeviceShift;
  174.         functionNumber = (cellHi & kPhysFunctionMask) >> kPhysFunctionShift;
  175.         registerNumber = (cellHi & kPhysRegisterMask) >> kPhysRegisterShift;
  176.         isAbsolute = ((cellHi & kPhysAbsolute) != 0) ? "abs" : "rel";
  177.         isPrefetch = ((cellHi & kPhysPrefetch) != 0) ? ", prefetch" : "";
  178.         isAlias    = ((cellHi & kPhysAlias)    != 0) ? ", alias" : "";
  179.  
  180.         switch ((cellHi & kPhysSpaceMask) >> kPhysSpaceShift)
  181.         {
  182.             case 0:    addressType = "Config";        break;
  183.             case 1:    addressType = "I/O";        break;
  184.             case 2:    addressType = "Mem";        break;
  185.             case 3:    addressType = "64-bit";        break;
  186.         }
  187.  
  188.         sprintf(
  189.             work,
  190.             "bus %u, dev %u, func %u, reg %u (%s) %s%s%s, %08lx %08lx %08lx",
  191.             busNumber,
  192.             deviceNumber,
  193.             functionNumber,
  194.             registerNumber,
  195.             addressType,
  196.             isAbsolute,
  197.             isPrefetch,
  198.             isAlias,
  199.             physAddressPropertyPtr[i].physHi,
  200.             physAddressPropertyPtr[i].physLo,
  201.             physAddressPropertyPtr[i].sizeLo
  202.         );
  203.  
  204.         status = MakeTwistDownSibling(twistDownSiblingSetPtr, kValueIndentLevel, strlen(work), (Ptr) work);
  205.     }
  206. }
  207.  
  208.  
  209. //--------------------------------------------------------------------------------
  210. #ifdef SYSF
  211. //    don't print out long hex dumps
  212. #endif
  213. TwistDownHdl FormatLargeProperty(const RegPropertyNameBuf foundProperty,
  214.         RegPropertyValueSize propertySize, const void *propertyValue);
  215. TwistDownHdl FormatLargeProperty(const RegPropertyNameBuf foundProperty,
  216.         RegPropertyValueSize propertySize, const void *propertyValue)
  217. {
  218.     TwistDownSiblingSet        twistDownSiblingSet;
  219.     OSErr                    status;
  220.     short                    i,offset,nBytesToDraw;
  221.     unsigned char            c;
  222.     unsigned char            *cp;
  223.     char                    work[256], thisValue[80];
  224. #define kBytesPerLine    16
  225. #ifdef SYSF
  226.     RegPropertyValueSize    savePropertySize = propertySize;
  227. #endif
  228.  
  229.     NewTwistDownSiblingSet(&twistDownSiblingSet);
  230.     if (IsAddressProperty(foundProperty) || IsRegProperty(foundProperty))
  231.         FormatPhysAddressProperty((PhysAddressPropertyPtr) propertyValue,
  232.             propertySize / sizeof (PhysAddressProperty), &twistDownSiblingSet);
  233.  
  234. #ifdef SYSF
  235.     if (propertySize > (kMaxLinesToPrint * kBytesPerLine))
  236.         propertySize = (kMaxLinesToPrint * kBytesPerLine);
  237. #endif
  238.     //    Even if it's a known property ("assigned-address"), format the raw hex data.
  239.     cp = (unsigned char *) propertyValue;
  240.     for (offset = 0; offset < propertySize; offset += kBytesPerLine)
  241.     {
  242.         if ((offset + kBytesPerLine) <= propertySize)
  243.             nBytesToDraw = kBytesPerLine;
  244.         else
  245.             nBytesToDraw = propertySize - offset;
  246.  
  247.         sprintf(work, "%04lx:", offset);
  248.         for (i = 0; i < nBytesToDraw; i++)
  249.         {
  250.             sprintf(thisValue, " %02x", cp[offset + i] & 0xFF);
  251.             strcat(work, thisValue);
  252.         }
  253.         for (; i < kBytesPerLine; i++)
  254.             strcat(work, "   ");
  255.         strcat(work, " ");
  256.         for (i = 0; i < nBytesToDraw; i++)
  257.         {
  258.             c = cp[offset + i];
  259.             if (c < ' ' || c > '~')
  260.                 c = '.';
  261.             thisValue[0] = c;
  262.             thisValue[1] = 0;
  263.             strcat(work, thisValue);
  264.         }
  265.         status = MakeTwistDownSibling(&twistDownSiblingSet, kValueIndentLevel, strlen(work), (Ptr) work);
  266.     }
  267. #ifdef SYSF
  268.     propertySize = savePropertySize;
  269. #endif
  270.     return (twistDownSiblingSet.firstElement);
  271. }
  272.  
  273.  
  274. //--------------------------------------------------------------------------------
  275. //    OneLineProperty is called by the display list creator to format
  276. //    "property : value" or "path : value" where the entire text fits
  277. //    on a single display line.
  278. Boolean OneLineProperty( const char *labelText, const TwistDownHdl valueElement, char *oneLineWork)
  279. {
  280.     int                        length;
  281.     Boolean                    oneLiner;
  282. #define VAL    (**valueElement)
  283.  
  284.     if (valueElement == NULL || VAL.nextElement != NULL)
  285.         oneLiner = FALSE;
  286.     else {
  287.         length = strlen(labelText) + strlen(" = ") + VAL.dataLength;
  288.         if (length > kOneLineFormatLength)
  289.             oneLiner = FALSE;
  290.         else {
  291.             oneLiner = TRUE;
  292.             sprintf(
  293.                 oneLineWork,
  294.                 "%s = %.*s",
  295.                 (char *) labelText,
  296.                 (int) VAL.dataLength,
  297.                 (char *) VAL.data
  298.             );
  299.         }
  300.     }
  301.     return (oneLiner);
  302. }
  303.  
  304. //--------------------------------------------------------------------------------
  305. //    Called for all property values.
  306. TwistDownHdl FormatThisProperty( const RegPropertyNameBuf foundProperty,
  307.         RegPropertyValueSize propertySize, const void *propertyValue)
  308. {
  309.     TwistDownHdl            propertyHdl;
  310.     Boolean                    oneLiner;
  311.     char                    work[kOneLineFormatLength + 1];
  312.  
  313.     oneLiner = FormatOneLineProperty(propertySize, propertyValue, work);
  314.  
  315.     if (oneLiner)
  316.         (void)  MakeTwistDownElement(NULL, kValueIndentLevel, strlen(work), (Ptr) work, &propertyHdl);
  317.     else
  318.         propertyHdl = FormatLargeProperty(foundProperty, propertySize, propertyValue);
  319.  
  320.     return (propertyHdl);
  321. }
  322.